package dao.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import dao.RoomDao;
import po.meeting.Room;

public class RoomDaoImpl implements RoomDao{
	private List <Room> rooms;
	
	public RoomDaoImpl() {
		rooms = new LinkedList<>();
	}
	@Override
	public boolean insert(Room t) {
		if(rooms.contains(t)) {
			return false;
		}
		return rooms.add(t);
	}

	@Override
	public boolean update(Room t) {
		int index = rooms.indexOf(t);
		if(index== -1) {
			return false;
		}else {
			rooms.set(index, t);
			return true;
		}
	}

	@Override
	public boolean delete(int id) {
		Iterator<Room> it = rooms.iterator();
		while(it.hasNext()) {
			Room next = it.next();
			if(next.getRoomId() == id) {
				it.remove();
				return true;
			}
		}
		return false;
	}

	@Override
	public Room selectById(int id) {
		Iterator<Room> it = rooms.iterator();
		while(it.hasNext()) {
			Room room = it.next();
			if(room.getRoomId() == id) {
				return room;
			}
		}
		return null;
	}

	@Override
	public List<Room> selectAll() {
		List<Room>copy = new LinkedList<>(rooms);
		return copy;
	}

}
